import {
  SAVE_ALLCHANNELS,
  SAVE_ARTICLELIST,
  SAVE_MOREARTICLELIST,
  SAVE_USRCHANNELS,
  DISPLAY_MOREACTION,
} from '../contants/home'

// 初始值
const initialState = {
  userChannel: [],
  allChannel: [],
  articles: {},
  feedbackAction: {
    // 控制弹出菜单的显示隐藏
    visible: false,
    // 当前反馈的目标文章ID
    articleId: '',
    // 频道id
    channelId: 0,
  },
}

export const home = (state = initialState, action) => {
  const { type, payload } = action
  switch (type) {
    case SAVE_USRCHANNELS:
      return {
        ...state,
        userChannel: payload,
      }
    case SAVE_ALLCHANNELS:
      return {
        ...state,
        allChannel: payload,
      }
    case SAVE_ARTICLELIST:
      return {
        ...state,
        articles: {
          ...state.articles,
          [payload.channelId]: {
            timestamp: payload.timestamp,
            list: payload.list,
          },
        },
      }
    case SAVE_MOREARTICLELIST:
      return {
        ...state,
        articles: {
          ...state.articles,
          [payload.channelId]: {
            timestamp: payload.timestamp,
            list: [...state.articles[payload.channelId].list, ...payload.list],
          },
        },
      }
    case DISPLAY_MOREACTION:
      return {
        ...state,
        feedbackAction: payload,
      }
    default:
      return state
  }
}
